import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:jet_rails/jr_constant/jr_urls.dart';
import 'package:jet_rails/jr_services/jr_http.dart';

export 'package:http/http.dart';

class JrApis {
  static final JrHttp _http = JrHttp();

  static JrHttp get http => _http;
  static set accessToken(String? token) => _http.accessToken = token;

  static Future<Map> getAppConfig(String version) async {
    return await _http.get<Map>(JrUrls.getAppConfig, query: {'ver': version});
  }

  /// Login
  static Future<Map> login({required int oauthType, required String token}) async {
    Map response = await _http.post<Map>(JrUrls.oauth,
        body: {'oauthType': '$oauthType', 'token': token},
        headers: {'Content-Type': 'application/x-www-form-urlencoded'});
    return response;
  }

  static Future<bool> isValidToken({required String token}) async {
    return await _http.post<bool>(JrUrls.isValidToken, body: {'token': token});
  }

  static Future<void> logout() async {
    await _http.post(JrUrls.logout);
  }

  static Future<void> deleteAccount() async {
    await _http.post(JrUrls.deleteAccount);
  }

  /// User
  static Future<List> broadcasterWallSearch() async {
    return await _http.post<List>(
      JrUrls.wallSearch,
      body: {
        "tag": "All",
        "category": "Popular",
        "isRemoteImageUrl": true,
        "isPageMode": true,
        "limit": 20,
        "page": 1,
      },
    );
  }

  static Future<Map> getUserInfo({required String userId}) async {
    return await _http.get<Map>(JrUrls.getUserInfo, query: {'userId': userId});
  }

  static Future<bool> saveUserInfo(Map<String, dynamic> map) async {
    return await _http.post<bool>(JrUrls.saveUserInfo, body: map);
  }

  static Future<Map> ossPolicy() async {
    return await _http.get<Map>(JrUrls.ossPolicy);
  }

  static Future<Map> updateAvatar(String ossPath) async {
    return await _http.post<Map>(JrUrls.updateAvatar, body: {'avatarPath': ossPath});
  }

  static Future<bool> followUser(String userId) async {
    try {
      EasyLoading.show();
      bool isSuccess = await _http.post<bool>(JrUrls.addFriend, body: {'followUserId': userId});

      return isSuccess;
    } finally {
      EasyLoading.dismiss();
    }
  }

  static Future<bool> unfollowUser(String userId) async {
    try {
      EasyLoading.show();
      bool isSuccess = await _http.post<bool>(JrUrls.unFriend, body: {'followUserId': userId});
      return isSuccess;
    } finally {
      EasyLoading.dismiss();
    }
  }

  static Future<List> getFollowedList() async {
    try {
      EasyLoading.show();
      return await _http.post<List>(JrUrls.friendsList, body: {'limit': 20, 'page': 1});
    } finally {
      EasyLoading.dismiss();
    }
  }

  static Future<bool> unblockUser(String userId) async {
    try {
      EasyLoading.show();
      bool isSuccess = await _http.post<bool>(JrUrls.removeBlock, body: {'blockUserId': userId});
      return isSuccess;
    } finally {
      EasyLoading.dismiss();
    }
  }

  static Future<bool> reportOrBlockUser(String userId, {String? reportComplain}) async {
    try {
      EasyLoading.show();
      final params = {
        'broadcasterId': userId,
      };
      // 'Pornographic', 'False gender', 'Fraud', 'Political sensitive', 'Other'
      if (reportComplain != null) {
        params.addAll({
          'complainCategory': 'Report',
          'complainSub': reportComplain,
        });
      } else {
        params.addAll({'complainCategory': 'Block'});
      }
      bool isSuccess = await _http.post<bool>(JrUrls.insertRecord, body: params);
      return isSuccess;
    } catch (e) {
      return false;
    } finally {
      EasyLoading.dismiss();
    }
  }

  static Future<List> getBlockedUsers() async {
    try {
      EasyLoading.show();
      return await _http.post<List>(JrUrls.blockList);
    } finally {
      EasyLoading.dismiss();
    }
  }

  /// Payment

  static Future<List> searchGoods() async {
    return await _http.post<List>(JrUrls.goodsSearch, body: {
      'isIncludeSubscription': false,
      'payChannel': 'IAP',
    });
  }

  static Future<Map> createTransaction(String goodsCode) async {
    Map map = await _http.post<Map>(JrUrls.rechargeCreate, body: {
      'entry': '',
      'source': '',
      'goodsCode': goodsCode,
      'payChannel': 'IAP',
    });
    return map;
  }

  static Future<dynamic> verifyTransaction(String orderNo, String payload, String transactionId) async {
    dynamic res = await _http.post<dynamic>(JrUrls.paymentIpa, body: {
      'orderNo': orderNo,
      'payload': payload,
      'transactionId': transactionId,
      'type': "1",
    });
    return res;
  }

  static Future<dynamic> consumeCoins(int coinsCount) async {
    dynamic res = await _http.post<dynamic>(JrUrls.reviewModeConsume, body: {
      "outlay": coinsCount,
      "source": "iap",
    });
    return res;
  }

  static Future<dynamic> mediaSearch(dynamic params) async {
    return await _http.post<dynamic>(JrUrls.mediaSearch, body: params);
  }

  static Future<dynamic> translate(dynamic params) async {
    final newHttp = JrHttp(baseURL: JrUrls.googleTranslateBaseUrl)..accessToken = _http.accessToken;
    return await newHttp.post(JrUrls.translate, body: params, wrap: false);
  }

  static Future<dynamic> eventTracking(dynamic params) async {
    final newHttp = JrHttp(baseURL: JrUrls.trackingBaseUrl)..accessToken = _http.accessToken;
    return await newHttp.post(JrUrls.eventTracking, body: params);
  }
}
